﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Adams.Public.Api.Controllers
{
    [EnsureHttps]
    public class DonationsController : Controller
    {
        public ActionResult HasValidCreditCard(Public.DataTransfer.MemberInfo input)
        {
            input.MemberID = Api.Security.GetMemberIDFromToken(input.Token);
            var card = DataAccess.Donations.GetCreditCard(input);
            var model = new DataTransfer.Donations.HasValidCreditCard();

            if (card.ID > 0)
            {
                if (DateTime.Now.Year < card.CardExpireYear.ToInt() ||
                    (DateTime.Now.Year == card.CardExpireYear.ToInt() && DateTime.Now.Month < card.CardExpireMonth.ToInt()))
                {
                    model.IsValid = true;
                }
            }

            return new JsonXmlResult(model);
        }

        public ActionResult GetBucket(Public.DataTransfer.Donations.GetBucketInput input)
        {
            if (string.IsNullOrEmpty(input.BucketID))
            {
                input.BucketID = DataAccess.Donations.GetDefaultBucketID().ToString();
            }

            var model = DataAccess.Donations.GetBucket(input);
            return new JsonXmlResult(model);
        }

        public ActionResult GetDonations(Public.DataTransfer.MemberInfo input)
        {
            input.MemberID = Api.Security.GetMemberIDFromToken(input.Token);
            var model = DataAccess.Donations.GetDonations(input);
            return new JsonXmlResult(model);
        }

        public ActionResult CancelDonation(DataTransfer.Web.Areas.Membership.Donations.Index.CancelInput input)
        {
            input.MemberID = Api.Security.GetMemberIDFromToken(input.Token);
            var model = new DataTransfer.Web.Areas.Membership.Donations.Index.CancelOutput();
            var status = DataAccess.Donations.GetDonationStatus(input.DonationID.ToInt());

            if (status == Shared.Entities.Donations.Status.NotConfirmed || status == Shared.Entities.Donations.Status.Pending)
            {
                DataAccess.Donations.CancelDonation(input);
                model.Success = true;
            }
            else
            {
                model.Success = false;
            }

            return new JsonXmlResult(model);
        }

        public ActionResult CancelRecurringDonation(DataTransfer.Web.Areas.Membership.Donations.Index.CancelRecurringInput input)
        {
            input.MemberID = Api.Security.GetMemberIDFromToken(input.Token);
            var model = new DataTransfer.Web.Areas.Membership.Donations.Index.CancelRecurringOutput();
            var status = DataAccess.Donations.GetRecurringDonationStatus(input.DonationID.ToInt());

            if (status == Shared.Entities.Donations.Status.NotConfirmed || status ==  Shared.Entities.Donations.Status.Pending || 
                status == Shared.Entities.Donations.Status.Complete || status == Shared.Entities.Donations.Status.Failed)
            {
                DataAccess.Donations.CancelRecurringDonation(input);
                model.Success = true;
            }
            else
            {
                model.Success = false;
            }

            return new JsonXmlResult(model);
        }
    }
}